import { View } from "../../../../mvc/core/View";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { GuildStructureConfig } from "../../../resource/GuildStructureConfig";
import { Cache } from "../../../cache/Cache";
import { EGuildPosition } from "../../../../../Message/Game/EGuildPosition";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GuildUtil } from "../info/GuildUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { GuildEventName } from "../GuildEventName";
import { DataEvent } from "../../../events/DataEvent";
import { RemainTimeTool } from "../../award/RemainTimeTool";
import { SGuildStructureInfo } from "../../../../../Message/Game/SGuildStructureInfo";
import { MsgManager } from "../../../manager/MsgManager";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { EventName } from "../../../mvc/EventName";
import { GameDefConfig } from "../../../resource/GameDefConfig";
import { GTextInput } from "../../../../../com/mui/controls/GTextInput";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
type int = number;
//class BaseDetailPenel
    
    export  class BaseDetailPenel extends View
    {
        constructor()
        {
            super();
            return;
        }

        protected createCommon(): void
        {
            this.currentFundsLabel = ObjCreate.createGTextField(Language.getString(57386), 340, 195, 220, 20, this);
            this.currentFundsInput = ObjCreate.createGTextInput(400, 192, 60, 20, this);
            this.currentFundsInput.setStyle("textFormat", GlobalStyle.textFormat6);
            this.currentFundsInput.editable = false;
            return;
        }

        public get maxLevel(): int
        {
            var loc1=0;
            if (this.structureInfo) 
            {
                loc1 = this.structureInfo.type.__value;
                this._maxLevel = GuildStructureConfig.instance.getMaxLevelByType(loc1);
            }
            return this._maxLevel;
        }

        protected runPublic(): void
        {
            this.initRight();
            this.checkFunds();
            this.updateStatus();
            this.dealUpgrading();
            this.registToolTip();
            this.updateResource();
            if (this.upgradeBtn) 
            {
                this.upgradeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onUpgradeBtnClickHandler);
            }
            if (this.accBtn) 
            {
                this.accBtn.toolTipData = Language.getString(57052);
                this.accBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAccBtnClickHandler);
            }
            return;
        }

        /* internal  */registToolTip(): void
        {
            if (this.consumeTxt) 
            {
                this.consumeTxt.toolTipData = Language.getString(57053);
            }
            return;
        }

        /* internal  */initRight(): void
        {
            var loc1=null;
            if (this.upgradeBtn) 
            {
                loc1 = Cache.instance.guild.myGuildInfo;
                if (loc1.position != EGuildPosition._EGuildLeader) 
                {
                    this.upgradeBtn.visible = false;
                }
                else 
                {
                    this.upgradeBtn.visible = true;
                }
            }
            return;
        }

        protected checkFunds(): void
        {
            var loc1=this.structureInfo.type.__value;
            var loc2=GuildStructureConfig.instance.getInfoByType(loc1, this.structureInfo.level);
            if (loc2) 
            {
                if (Cache.instance.guild.myGuildInfo.money < loc2.needMoney) 
                {
                    this.fundsInput.setStyle("textFormat", new GTextFormat(FontUtil.songtiName, 12, 16711680));
                    this._fundsEnough = false;
                }
                else 
                {
                    this.fundsInput.setStyle("textFormat", GlobalStyle.textFormat6);
                    this._fundsEnough = true;
                }
                this.fundsInput.text = loc2.needMoney.toString();
            }
            return;
        }

        protected updateStatus(): void
        {
            if (this.structureInfo.level == this.maxLevel || this._fundsEnough == false) 
            {
                this.statusText.htmlText = HTMLUtil.addColor(Language.getString(57044), "#ff0000");
                this.upgradeBtn.visible = false;
            }
            else if (GuildUtil.isUpgrading(this.structureInfo)) 
            {
                this.statusText.htmlText = HTMLUtil.addColor(Language.getString(57054), "#00ff00");
                this.upgradeBtn.visible = false;
            }
            else 
            {
                this.statusText.htmlText = HTMLUtil.addColor(Language.getString(57055), "#00ff00");
                this.upgradeBtn.visible = true;
            }
            return;
        }

        protected dealUpgrading(): void
        {
            if (GuildUtil.isUpgrading(this.structureInfo)) 
            {
                if (this.remainTimeTxt == null) 
                {
                    this.remainTimeTxt = ObjCreate.createTextField("", 130, 5, 220, 20, this);
                }
                if (this.accBtn == null) 
                {
                    this.accBtn = ObjCreate.createGButton(Language.getString(57056), 345, 2, 65, 24, this);
                }
                Dispatcher.addEventListener(GuildEventName.StrutureRemainTimeUpdate, this.onStrutureRemainTimeUpdateHanler);
            }
            else 
            {
                if (this.remainTimeTxt && this.contains(this.remainTimeTxt)) 
                {
                    this.removeChild(this.remainTimeTxt);
                }
                if (this.accBtn && this.contains(this.accBtn)) 
                {
                    this.removeChild(this.accBtn);
                }
                Dispatcher.removeEventListener(GuildEventName.StrutureRemainTimeUpdate, this.onStrutureRemainTimeUpdateHanler);
            }
            return;
        }

        /* internal  */onStrutureRemainTimeUpdateHanler(arg1: DataEvent): void
        {
            var loc1=arg1.data as number;
            if (loc1 == 0) 
            {
                this.remainTimeTxt.text = "";
                return;
            }
            if (this.remainTimeTxt) 
            {
                this.remainTimeTxt.text = RemainTimeTool.getRemainTimeBySeconds(loc1);
            }
            return;
        }

        /* internal  */onUpgradeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var event: Object /* flash.events.MouseEvent */;
            var buidingStructure: SGuildStructureInfo;
            var name: string;
            var htmlContent: string;
            var onClickHandler: Function;

            var loc1;
            name = null;
            htmlContent = null;
            event = arg1;
            buidingStructure = Cache.instance.guild.getBuildingStructureInfo();
            if (buidingStructure) 
            {
                MsgManager.showRollTipsMsg(Language.getString(57057));
                return;
            }
            if (this.structureInfo) 
            {
                onClickHandler = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUpdateStruture, structureInfo.type));
                        Alert.resetToDefault();
                    }
                    return;
                }
                if (!this._fundsEnough) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57058));
                    return;
                }
                name = GameDefConfig.instance.getGuildBuildingName(this.structureInfo.type.__value).split(" ").join("");
                htmlContent = Language.getStringByParam(57059, name, this.structureInfo.level + 1);
                Alert.show(htmlContent, null, Alert.OK | Alert.CANCEL, null, onClickHandler);
            }
            return;
        }

        /* internal  */onAccBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.structureInfo) 
            {
                Dispatcher.dispatchEvent(new DataEvent(GuildEventName.AccWindowOpen, this.structureInfo.type));
            }
            return;
        }

        /* internal  */updateResource(): void
        {
            this.currentFundsInput.text = Cache.instance.guild.myGuildInfo.money.toString();
            return;
        }

        protected /* var */remainTimeTxt: Object /* flash.text.TextField */;

        protected /* var */statusText: Object /* flash.text.TextField */;

        protected /* var */fundsInput: GTextInput;

        protected /* var */upgradeBtn: GButton;

        protected /* var */accBtn: GButton;

        protected /* var */consumeTxt: GTextFiled;

        protected /* var */currentFundsLabel: GTextFiled;

        protected /* var */currentFundsInput: GTextInput;

        protected /* var */structureInfo: SGuildStructureInfo;

        /* internal */ /* var */_maxLevel: int;

        /* internal */ /* var */_fundsEnough: Boolean=false;
    }
